package com.lmaos.basetools.control;

import java.util.HashMap;
import java.util.Map;

import com.lmaos.basetools.base.SNumberUtils;

public class SResult {
	public static final int SUCCESS_STATE = 0;
	public static final int EXCEPTION_STATE = -1;
	public static final Map<Class, Integer> errState = new HashMap<Class, Integer>();
	public static final void setErrState(Class<? extends Throwable> e,int state){
		errState.put(e, state);
	}
	public static final int getErrState(Exception e){
		if(e==null){
			return SUCCESS_STATE;
		}
		Integer code = errState.get(e.getClass());
		if(code == null){
			code = EXCEPTION_STATE;
		}
		return code;
	}
	private Object result;
	private int state;
	private Exception e;
	public Object getResult() {
		return result;
	}
	public <T>T getResultObject() {
		if(result == null){
			return null;
		}
		return (T)result;
	}
	public int getInt(){
		return SNumberUtils.toInt(result);
	}
	public long getLong(){
		return SNumberUtils.toLong(result);
	}
	public String getString(){
		if(result == null){
			return null;
		}
		return String.valueOf(result);
	}
	public short getShort(){
		return SNumberUtils.toShort(result);
	}
	public double getDouble(){
		return SNumberUtils.toDouble(result);
	}
	public float getFloat(){
		return SNumberUtils.toFloat(result);
	}
	
	public void setResult(Object result) {
		this.result = result;
	}
	public Exception getE() {
		return e;
	}
	public void setE(Exception e) {
		this.e = e;
		this.state = getErrState(e);
	}
	public void setState(int state) {
		this.state = state;
	}
	public int getState() {
		return state;
	}
	
}
